Change auto-incrementing integer ID's with auto generated UUID's. #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In this implementation, the auto-generation of UUIDs is handled by the PostgreSQL database, specifically within the insert_memory method in the PostgresClient class. When a new record is inserted, the database generates a unique UUID by default, thanks to the
id uuid DEFAULT uuid_generate_v4()
line in the table creation query. The generated UUID is then returned by theRETURNING id; SQL
statement and fetched byreturn self.cur.fetchone()[0]
. This ensures that each memory object has a unique identifier without requiring manual generation.NOTE: This implementation has many broken tests that will need to be fixed.
The main changes in the create_memory function within the agentmemory/main.py file are as follows:
ID Generation Logic:
The original implementation generated an ID based on the count of documents in the collection and padded it to make it 16 digits long. In the new implementation, this logic has been removed. Instead, the ID is auto-generated by PostgreSQL as a UUID when the record is inserted.
Old:
New:
The ID generation logic is removed, and the ID (if not provided) is generated by the database.
Insert Operation:
The old implementation used a method called upsert to insert the document into the collection. The new implementation uses a method called add.
Old:
New:
Returning ID:
In the old implementation, the function returned the ID generated or provided. In the new implementation, it assumes that the add method appends the newly generated UUID to the ids list, and that is returned.
Old:
New:
Metadata Handling:
The new version also includes more robust metadata handling, especially for boolean and other non-string types.
By making these changes, the create_memory function now relies on the PostgreSQL database for UUID generation, which is generally more reliable and efficient than manual generation. It also aligns better with industry best practices.
Suggestion for Robust ID Handling
While the new implementation leverages PostgreSQL for UUID generation, it might be beneficial to retain the flexibility of allowing users to specify an ID if they wish. This can be achieved by adding a conditional check before the add method call in the create_memory function.
Here's a suggested modification for create_memory to make it robust to both user-provided IDs and auto-generated UUIDs:
By implementing this modification, you ensure that if the user provides an ID, that will be used; otherwise, a UUID will be generated by PostgreSQL. This approach preserves the robustness of the original implementation while benefiting from the advantages of database-managed UUIDs.
Resources:
Supabase uuid-ossp extension guide